import { generateUUID } from "@/utils";
import { getProductModelAll } from "@/api/modules/carType";
import { getProjectListCodeAll } from "@/api/modules/project";
import { getProductCategoryPartAll } from "@/api/modules/carType";
import { deleteFileApi } from "@/api/modules/upload";
import { useDict } from "@/hooks/useDict";
import { imageType, videoType, fileType } from "../enum";
const images = imageType.map(item => item.fixedList).flat();
const videos = videoType.map(item => item.fixedList).flat();
const files = fileType.map(item => item.fixedList).flat();

const acceptConst: any = {
  1: "image/jpeg,image/png,image/gif",
  2: "video/mp4",
  3: "*"
};
export const useUtils = () => {
  const transformDataReq = (data: any, appearance: any, testVideo: any) => {
    data = JSON.parse(JSON.stringify(data));
    data.productDataList = data.productDataList.filter(item => item.type == 3);
    appearance.forEach((item: any) => {
      data.productDataList = data.productDataList.concat(item.productDataList);
    });
    testVideo.forEach((item: any) => {
      data.productDataList = data.productDataList.concat(item.productDataList);
    });

    // data.productParts = data.productParts.filter(item => item.partId);
    data.productParts.forEach((item: any) => {
      delete item.uuid;
      delete item.lonely;
      delete item.previewLink;
    });
    data.productDataList.forEach((item: any) => {
      delete item.uuid;
      delete item.lonely;
      delete item.fixed;
      delete item.previewLink;
    });
    data.assemblyInfos.forEach((item: any) => {
      delete item.uuid;
      delete item.lonely;
      delete item.previewLink;
    });
    return data;
  };
  const transformDataRes = (data: any) => {
    const productFixUuids = [...images, ...videos, ...files];
    data = JSON.parse(JSON.stringify(data));
    data.oaLaserCodeList.forEach((item: any) => {
      item.uuid = generateUUID();
    });
    data.productDataList.forEach((item: any) => {
      const obj = productFixUuids.find(itm => itm.remark === item.remark);
      if (obj) {
        item.uuid = obj.uuid;
      } else {
        item.uuid = generateUUID();
      }
    });
    data.assemblyInfos.forEach((item: any) => {
      item.uuid = generateUUID();
    });
    return data;
  };
  // 页面基础数据获取
  const productBaseData = async (options: any) => {
    const { selectList } = options;
    selectList.value["projectList"] = await getProject();
    selectList.value["projectList"] = selectList.value["projectList"].map((item: any) => {
      item.nameClone = item.code + "-" + item.name;
      return item;
    });
    // 获取外观和测试视频基础数据、关键零部件
    const { getDicList } = useDict();
    const dictionary = window.staticConfig.dictionary;
    let originList = [dictionary.appearance, dictionary.testVideo, dictionary.keyPart, dictionary.accessoryType];
    // 获取数据
    const data: any = await getDicList({ parentIdList: originList.join(",") });
    let appearanceTestVideo: any = {};
    let keyPart: any = {};
    const list = Object.keys(data);
    list.forEach((item, index) => {
      if (!index) {
        selectList.value["keyPartList"] = data[item];
      } else if (index === 1) {
        keyPart[item] = data[item];
      } else {
        appearanceTestVideo[item] = data[item];
      }
    });
    // 二级所有ID
    let parentIdList: any = [];
    Object.values(appearanceTestVideo).forEach(item => {
      item.forEach((item2: any) => {
        parentIdList.push(item2.param);
      });
    });
    // 获取二级所有数据
    const result = await getDicList({ parentIdList: parentIdList.join(",") });
    return { result, appearanceTestVideo, keyPart };
  };
  // 获取所有车型
  const getProductModelAllFn = async (typeId: string) => {
    const { data } = await getProductModelAll({ typeId });
    return data;
  };
  // 获取所有项目
  const getProject = async () => {
    const { data } = await getProjectListCodeAll();
    return (
      (data &&
        data.map((item: any) => {
          item.nameClone = item.code + "-" + item.name;
          return item;
        })) ||
      []
    );
  };
  const setInfo = (data: string | Array<any>) => {
    if (Array.isArray(data)) {
      data = data.length ? data : "";
    }
    return data || "--";
  };
  // 是否是默认的视频、图片、文件
  const isDefault = (id: string, list: Array<any>) => {
    if (!id || !list) return false;
    return list.includes(id);
  };

  const getAcceptType = (id: string) => {
    return acceptConst[id];
  };

  const fittingsFn = async (typeId: string) => {
    const { data } = await getProductCategoryPartAll(
      {
        typeId
      },
      { noLoading: true }
    );
    const obj: any = {};
    obj[typeId] = data;
    return obj;
  };

  const deleteFile = async (path: string) => {
    await deleteFileApi({ path });
  };

  return {
    transformDataRes,
    transformDataReq,
    getProductModelAllFn,
    getProject,
    setInfo,
    isDefault,
    getAcceptType,
    fittingsFn,
    acceptConst,
    productBaseData,
    deleteFile
  };
};
